package com.example.demo.controller.data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/bill")
public class BillAnalysisController {

    @Autowired
    private BillAnalysisService billAnalysisService;

    @PostMapping("/analyze")
    public ResponseEntity<byte[]> analyzeBill(@RequestParam("file") MultipartFile file,
                                              @RequestParam(value = "startTime", required = false) Long startTime,
                                              @RequestParam(value = "endTime", required = false) Long endTime) throws IOException {
        // 转换时间戳为Date对象
        Date startDate = startTime != null ? new Date(startTime) : null;
        Date endDate = endTime != null ? new Date(endTime) : null;

        byte[] bytes = billAnalysisService.analyzeBill(file, startDate, endDate);
        if(bytes==null){
            //提示前端该时间段无数据
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("该时间段无数据".getBytes());
        }
        HttpHeaders headers = new HttpHeaders();

        // 如果提供了时间段，添加时间段信息到文件名
        String fileName = "账单分析结果";
        if (startTime != null && endTime != null) {
            String startStr = new SimpleDateFormat("yyyyMMdd").format(startDate);
            String endStr = new SimpleDateFormat("yyyyMMdd").format(endDate);
            fileName += "_" + startStr + "-" + endStr;
        }


        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.parseMediaType("application/zip"));
        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }
    @PostMapping("/anchor-data")
    public ResponseEntity<byte[]> getAnchorData(@RequestParam("billFile") MultipartFile billFile,
                                                @RequestParam("conditionFile") MultipartFile conditionFile) throws IOException {
        // 1. 读取账单数据
        List<BillDataDto> billDataList = excelService.readExcel(billFile);

        // 2. 读取退款数据
        List<RefundDataDto> refundDataList = excelService.readRefundExcels(billFile);

        // 3. 读取条件数据
        List<AnchorConditionDto> conditionList = excelService.readConditionExcel(conditionFile);

        // 4. 生成主播数据Excel压缩包
        byte[] bytes = excelService.generateAnchorDataExcelZip(billDataList, refundDataList, conditionList);

        // 5. 设置响应头并返回
        HttpHeaders headers = new HttpHeaders();
        String fileName = "主播销售数据_" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
        headers.setContentDispositionFormData("attachment", fileName + ".zip");
        headers.setContentType(MediaType.parseMediaType("application/zip"));

        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }

    // 需要添加ExcelService的注入
    @Autowired
    private ExcelService excelService;
}
